$(function () {
    console.log(">>>>>>>>>popup init");
    // 加载设置
    chrome.storage.local.get({
        content: '',
        taskStatus: 0,
        proxy: '',
        proxy_key: '',
    }, function (items) {
        console.log("初始化：", JSON.stringify(items));
        document.getElementById("content").value = items.content;
        document.getElementById("proxy").value = items.proxy;
        document.getElementById("proxy_key").value = items.proxy_key;
        if (items.taskStatus == 0) {
            document.getElementById("cancel").style.display = 'none';
        } else {
            if (items.taskStatus == 1) {
                document.getElementById("title").innerHTML = '任务进行中……';
                document.getElementById("zanting").style.display = 'inline';
            } else if (items.taskStatus == 2) {
                document.getElementById("title").innerHTML = '任务已暂停';
                document.getElementById("kaishi").style.display = 'inline';
            }
            document.getElementById("submit_info").style.display = 'none';
            document.getElementById("cancel").style.display = 'inline';
            document.getElementById("content").disabled = 'disabled';
            document.getElementById("proxy").disabled = 'disabled';
            document.getElementById("proxy_key").disabled = 'disabled';
        }
    });

    //取消
    $('#cancel').click(function () {
        sendMessageToContentScript({
            cmd: "cancel",
            taskStatus: 0//0:停止，1开始，2暂停
        }, (response) => {
            if (response) {
                if (response.code == 0) {
                    $('#cancel').hide();
                    $('#zanting').hide();
                    $('#kaishi').hide();
                    $('#submit_info').show();
                    $("#content").removeAttr("disabled");
                    $("#proxy").removeAttr("disabled");
                    $("#proxy_key").removeAttr("disabled");
                    $("#title").html('创建发送任务');
                    alert("已取消发送");
                } else {
                    alert(response.msg);
                }
            };
        });
    });
    //暂停
    $('#zanting').click(function () {
        sendMessageToContentScript({
            cmd: "zanting",
            taskStatus: 2//0:停止，1开始，2暂停
        }, (response) => {
            if (response) {
                if (response.code == 0) {
                    $('#zanting').hide();
                    $("#kaishi").show();
                    $("#title").html('任务已暂停');
                } else {
                    alert(response.msg);
                }
            };
        });
    });
    //开始
    $('#kaishi').click(function () {
        sendMessageToContentScript({
            cmd: "send_message",
            url: 'list',
            taskStatus: 1//0:停止，1开始，2暂停
        }, (response) => {
            if (response) {
                if (response.code == 0) {
                    $('#kaishi').hide();
                    $("#zanting").show();
                    $("#title").html('任务进行中……');
                } else {
                    alert(response.msg);
                }
            };
        });
    });

    //创建任务和开始
    $('#submit_info').click(() => {
        let content = $("#content").val();
        let proxy = $("#proxy").val();
        let proxy_key = $("#proxy_key").val();
        if (!content) {
            alert("发送文案不能为空");
            return
        }
        if (confirm("开始群发私信？\n\r私信开始后：\n\r1.请一直停留在此页面，不要切换到其他页面。\n\r2.可通过关闭页面终止私信。")) {
            sendMessageToContentScript({
                cmd: "save_send_info",
                content,
                proxy,
                proxy_key,
                height: 0,
                // height: 3822,//用于断点续传
                // user_id: '975400286719979550',
                taskStatus: 1,//0:停止，1开始，2暂停
                captchaCount: 0
            }, (response) => {
                if (response) {
                    if (response.code == 0) {
                        $("#submit_info").hide();
                        $("#zanting").show();
                        $("#cancel").show();
                        $("#content").attr("disabled", 'disabled');
                        $("#proxy").attr("disabled", 'disabled');
                        $("#proxy_key").attr("disabled", 'disabled');
                        $("#title").html('任务进行中……');
                        window.close();
                    } else {
                        alert(response.msg);
                    }
                };
            });
        } else {
            return;
        }
    });
});

// 获取当前选项卡ID
function getCurrentTabId(callback) {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        if (callback) callback(tabs.length ? tabs[0].id : null);
    });
}

// 向content-script主动发送消息
function sendMessageToContentScript(message, callback) {
    getCurrentTabId((tabId) => {
        chrome.tabs.sendMessage(tabId, message, function (response) {
            if (callback) callback(response);
        });
    });
}

// 向content-script注入JS片段
function executeScriptToCurrentTab(code) {
    getCurrentTabId((tabId) => {
        chrome.tabs.executeScript(tabId, { code: code });
    });
}